package application.services;

import application.exceptions.ItemNotFoundException;
import application.model.AppliancesModel;
import application.model.modify.ApplianceModel;
import application.translators.Translator;
import application.translators.impl.AppliancesTranslator;
import dao.AppliancesDao;
import mappings.Appliances;
import org.hibernate.exception.ConstraintViolationException;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Path("appliances")
public class AppliancesService
{
    private AppliancesDao appliancesDao = new AppliancesDao();
    private Translator<List<Appliances>, AppliancesModel> translator = new AppliancesTranslator();

    @Path("getAllAppliances")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public AppliancesModel getAllAppliances()
    {
        return translator.translate(appliancesDao.getAllAppliances());
    }

    @Path("addAppliance")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public ApplianceModel addNewAppliance(@FormParam("name") String name)
    {
        ApplianceModel applianceModel = new ApplianceModel();

        try
        {
            Appliances appliance = appliancesDao.addAppliance(name);
            applianceModel.setId(appliance.getId());
            applianceModel.setName(appliance.getName());
            applianceModel.setStatus(0);
        }
        catch (ConstraintViolationException e)
        {
            // lapiemy wyjatek rzucany przez hibernate'a dla unique constraint - ustawiamy status 1 (rekord istnieje)
            applianceModel.setStatus(1);
        }
        catch (Exception e)
        {
            // lapiemy wszystkie inne exceptiony i ustawiamy status na 2 (czyli cos sie wyjebalo, ale nie wiadomo co :P)
            applianceModel.setStatus(2);
        }

        return applianceModel;
    }

    @Path("editAppliance")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public ApplianceModel editAppliance(@FormParam("id") Integer id, @FormParam("name") String name)
    {
        ApplianceModel applianceModel = new ApplianceModel();

        try
        {
            Appliances appliance = appliancesDao.editAppliance(id, new Appliances(name));
            applianceModel.setName(appliance.getName());
            applianceModel.setId(appliance.getId());
            applianceModel.setStatus(0);
        }
        catch (ItemNotFoundException e)
        {
            applianceModel.setStatus(1); // 1 = nie znaleziono rekordu o danym id
        }
        catch (Exception e)
        {
            applianceModel.setStatus(2);
        }

        return applianceModel;
    }

    @Path("deleteAppliance")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public ApplianceModel deleteAppliance(@FormParam("id") Integer id)
    {
        ApplianceModel applianceModel = new ApplianceModel();

        try
        {
            appliancesDao.deleteAppliance(id);
            applianceModel.setStatus(0);
        }
        catch (ItemNotFoundException e)
        {
            applianceModel.setStatus(1); // 1 = nie znaleziono rekordu o danym id
        }
        catch (Exception e)
        {
            applianceModel.setStatus(2);
        }

        return applianceModel;
    }
}
